﻿namespace Catel.Articles._08___WP7_Camera.Test.ViewModels
{
    using System;
    using System.Windows;
    using System.Windows.Media.Imaging;
    using Data;
    using IoC;
    using MVVM.Services;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using _08___WP7_Camera.Data;
    using _08___WP7_Camera.ViewModels;
    using NavigationService = MVVM.Services.Test.NavigationService;
    using CameraService = MVVM.Services.Test.CameraService;

    [TestClass]
    public class MainPageViewModelTest
    {
        private NavigationService _testNavigationService;
        private CameraService _testCameraService;

        [TestInitialize]
        public void Initialize()
        {
            var serviceLocator = ServiceLocator.Instance;

            if (_testNavigationService == null)
            {
                _testNavigationService = new NavigationService();
                serviceLocator.RegisterInstance<INavigationService>(_testNavigationService);
            }

            if (_testCameraService == null)
            {
                var testImage = new BitmapImage();
                var streamResourceInfo = Application.GetResourceStream(new Uri("/Catel.Articles.08 - WP7 Camera;component/Resources/Images/Catel.png", UriKind.RelativeOrAbsolute));
                testImage.CreateOptions = BitmapCreateOptions.None;
                testImage.SetSource(streamResourceInfo.Stream);
                _testCameraService = new CameraService(testImage);
                serviceLocator.RegisterInstance<ICameraService>(_testCameraService);
            }
        }

        [TestMethod]
        public void Flick_NoImagesAvailable()
        {
            var serviceLocator = ServiceLocator.Instance;
            var photoRepository = new TestPhotoRepository(0);
            serviceLocator.RegisterInstance<IPhotoRepository>(photoRepository);

            var vm = new MainPageViewModel();

            Assert.IsFalse(vm.Flick.CanExecute(new FlickData(FlickMovement.LeftToRight)), "Flick from left to right is never allowed");
            Assert.IsFalse(vm.Flick.CanExecute(new FlickData(FlickMovement.RightToLeft)));
        }

        [TestMethod]
        public void Flick_ImagesAvailable()
        {
            var serviceLocator = ServiceLocator.Instance;
            var photoRepository = new TestPhotoRepository(1);
            serviceLocator.RegisterInstance<IPhotoRepository>(photoRepository);

            var vm = new MainPageViewModel();

            Assert.IsFalse(vm.Flick.CanExecute(new FlickData(FlickMovement.LeftToRight)), "Flick from left to right is never allowed");
            Assert.IsTrue(vm.Flick.CanExecute(new FlickData(FlickMovement.RightToLeft)));
        }

        [TestMethod]
        public void View_NoImagesAvailable()
        {
            var serviceLocator = ServiceLocator.Instance;
            var photoRepository = new TestPhotoRepository(0);
            serviceLocator.RegisterInstance<IPhotoRepository>(photoRepository);

            var vm = new MainPageViewModel();

            Assert.IsFalse(vm.View.CanExecute());
        }

        [TestMethod]
        public void View_ImagesAvailable()
        {
            var serviceLocator = ServiceLocator.Instance;
            var photoRepository = new TestPhotoRepository(1);
            serviceLocator.RegisterInstance<IPhotoRepository>(photoRepository);

            _testNavigationService.ClearLastNavigationInfo();

            var vm = new MainPageViewModel();

            Assert.IsTrue(vm.View.CanExecute());
            vm.View.Execute();
            Assert.AreEqual(typeof(PhotoViewModel).FullName, _testNavigationService.LastNavigationUri, "Should have navigated to PhotoView");
            Assert.AreEqual(0, _testNavigationService.LastNavigationParameters["id"]);
        }

        [TestMethod]
        public void About_Navigating()
        {
            var serviceLocator = ServiceLocator.Instance;
            var photoRepository = new TestPhotoRepository(1);
            serviceLocator.RegisterInstance<IPhotoRepository>(photoRepository);

            _testNavigationService.ClearLastNavigationInfo();

            var vm = new PhotoViewModel();

            vm.About.Execute();
            Assert.AreEqual(typeof(AboutViewModel).FullName, _testNavigationService.LastNavigationUri, "Should have navigated to AboutView");
        }
    }
}
